package cn.wolfcode.p2p.business.service.impl;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.domain.LoginInfo;

import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.business.domain.RechargeOnline;
import cn.wolfcode.p2p.business.mapper.RechargeOnlineMapper;
import cn.wolfcode.p2p.business.service.IAccountFlowService;
import cn.wolfcode.p2p.business.service.IRechargeOnlineService;
import cn.wolfcode.p2p.hosting.service.IHostingService;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class RechargeOnlineServiceImpl implements IRechargeOnlineService {
        @Autowired
        private RechargeOnlineMapper rechargeOnlineMapper;
        @Autowired
        private IHostingService hostingService;
        @Autowired
        private IAccountFlowService accountFlowService;
        @Autowired
        private IAccountService accountService;
        @Autowired
        private ApplicationContext act;

        @Override
        public String apply(BigDecimal amount) {
                RechargeOnline ro = new RechargeOnline();
                String code = UUID.randomUUID().toString();
                ro.setTradeCode(code);
                ro.setAmount(amount);
                ro.setApplyTime(new Date());
                LoginInfo loginInfo = UserContext.getLoginInfo();
                ro.setApplier(loginInfo);
                rechargeOnlineMapper.insert(ro);
                //托管接口
                String result = hostingService.hostingRecharge(ro);
                return result;

        }

        @Override
        public RechargeOnline selectByTradeCode(String tradeCode) {
                return rechargeOnlineMapper.selectByTradeCode(tradeCode);
        }

        @Override
        public String resultHandler(Map<String, String> stringStringMap) {
                String tradeCode = stringStringMap.get("out_trade_no");
                RechargeOnline ro = selectByTradeCode(tradeCode);
                if(ro==null){
                        return "error";
                }
                String status = stringStringMap.get("deposit_status");
                if(status.equals("PROCESSING")){
                        return "ERROR";
                }else if(status.equals("SUCCESS")){
                        ro.setTradeTime(new Date());
                        ro.setRemark("充值成功,充值金额:"+stringStringMap.get("deposit_amount"));
                        ro.setState(RechargeOnline.STATE_SUCCESS);
                        Account account = accountService.getByid(ro.getApplier().getId());
                        account.addUsableAmount(ro.getAmount());
                        accountService.update(account);
                        rechargeOnlineMapper.updateByPrimaryKey(ro);
                        accountFlowService.createRechargeOnline(account,ro.getAmount());
                        return "SUCCESS";
                }else if(status.equals("FAILED")){
                        ro.setTradeTime(new Date());
                        ro.setRemark("充值失败,充值金额:"+stringStringMap.get("deposit_amount"));
                        ro.setState(RechargeOnline.STATE_REJECT);
                        rechargeOnlineMapper.updateByPrimaryKey(ro);
                        return "SUCCESS";
                }
                return "ERROR";

        }
}
